package com.developer.fans;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;

import com.developer.fans.info.AppStoreInfoView;
import com.developer.fans.upgrader.UpgraderView;

import java.util.List;


public class MainActivity extends Activity {
    public static class CrackItemView extends FrameLayout implements View.OnClickListener {
        private String title = null;

        public CrackItemView(String title) {
            super(CrackApplication.getContext());
            this.title = title;
        }

        protected void setContentView(View view) {
            addView(view);
        }

        public final String getTitle() {
            return title;
        }

        @Override
        public void onClick(View v) {

        }
    }

    private TableLayout root = null;

    private final CrackItemView[] views = {new AppStoreInfoView(), new UpgraderView(),
            new CrackItemView(CrackApplication.getContext().getResources().getString(R.string.restart)) {
                @Override
                public void onClick(View v) {
                    Context mContext = CrackApplication.getContext();
                    ActivityManager activityManager = (ActivityManager) mContext
                            .getSystemService(Context.ACTIVITY_SERVICE);
                    // 获取系统中所有正在运行的进程
                    List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
                    // 获取当前activity所在的进程
                    for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
                        String processName = appProcessInfo.processName;
                        if (processName.startsWith("com.tianci.appstore")) {
                            Log.d("fans", "reStartAppStore kill process name:" + processName);
                            activityManager.killBackgroundProcesses(processName);
                        }
                    }
                    Log.d("fans", "startAppStore !!");
                    Intent intent = new Intent();
                    intent.setAction("coocaa.intent.action.APP_STORE_HOME");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            }};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SkyConstants.init(this);

        root = new TableLayout(CrackApplication.getContext());
        root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


        ScrollView sv = new ScrollView(CrackApplication.getContext());
        sv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        sv.setBackgroundResource(R.drawable.game_center_bg);
        sv.addView(root);

        for (CrackItemView v : views) {
            TableRow row = new TableRow(CrackApplication.getContext());
            row.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
            row.addView(createButton(v.getTitle(), v));
            row.addView(v);
            root.addView(row);
        }
        setContentView(sv);
    }

    private Button createButton(String title, View.OnClickListener l) {
        Button btn = new Button(CrackApplication.getContext());
        btn.setText(title);
        btn.setTextSize(SkyConstants.Div(36));
        btn.setBackgroundResource(R.drawable.button_selector);
        btn.setTextColor(0xffffffff);
        btn.setOnClickListener(l);
        return btn;
    }
}
